from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, FIRST_COMPLETED

import requests
import ConnectMySql
import numpy

proxy = {'http': '33.33.33.10:8118'}
conn, cursor = ConnectMySql.openConn()


def getMusic(url):
    requests.adapters.DEFAULT_RETRIES = 20
    s = requests.session()
    s.keep_alive = False
    response = requests.get(url, proxy)
    result = eval(response.text)  # 将字符串转成dict格式
    print(f"{url} result: {result}")
    # response.close()
    for i in range(len(result["abslist"])):
        cursor.execute("select musicRid from music")
        musicArray = cursor.fetchall()
        musicArray = numpy.array(musicArray)
        if result["abslist"][i]["MUSICRID"] not in musicArray:
            cursor.execute(
                "insert into music(musicRid,songName,artist,artistId,album,albumId,score) VALUES (%s,%s,%s,%s,%s,%s,%s)",
                (result["abslist"][i]["MUSICRID"], result["abslist"][i]["SONGNAME"], result["abslist"][i]["ARTIST"],
                 result["abslist"][i]["ARTISTID"], result["abslist"][i]["ALBUM"], result["abslist"][i]["ALBUMID"],
                 result["abslist"][i]["SCORE100"],))
            conn.commit()
            print(result["abslist"][i]["SONGNAME"])


if __name__ == '__main__':
    executor = ThreadPoolExecutor(max_workers=10)

    cursor.execute("select sname from singer where sid not in (select artistId from music)")
    artistArray = cursor.fetchall()
    artistArray = numpy.array(artistArray)
    # cursor.execute("select sName from singer")
    # singerArray = cursor.fetchall()
    # singerArray = numpy.array(singerArray)
    # for i in range(len(artistArray)):
    #     # if singerArray[i] not in artistArray:
    #     url = "http://search.kuwo.cn/r.s?all=%s&ft=music&itemset=web_2013&client=kt&pn=0&rn=5&rformat=json&encoding=utf8" % \
    #           artistArray[i]
    #     # url = "http://search.kuwo.cn/r.s?all=毛不易&ft=music&itemset=web_2013&client=kt&pn=0&rn=300&rformat=json&encoding=utf8"
    #     # getMusic(url)
    #     executor.submit(getMusic, url)

    all_task = [executor.submit(getMusic,
                                # f'http://search.kuwo.cn/r.s?all={artist}&ft=music&itemset=web_2013&client=kt&pn=0&rn=5&rformat=json&encoding=utf8')
                                f'https://search.kuwo.cn/r.s?all={artist}ft=music&rformat=json&encoding=utf8')
                for artist in artistArray]

    wait(all_task, return_when=ALL_COMPLETED)

    ConnectMySql.closeConn(conn, cursor)
